package com.vcaresolution.logistic.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vcaresolution.logistic.form.CityForm;
import com.vcaresolution.logistic.pojo.City;
import com.vcaresolution.logistic.to.CityTO;

@Service
public class CityServiceImpl extends BaseService implements CityService{

	private City populateCityPojo(CityForm sectorForm){
		City sector = new City();
		BeanUtils.copyProperties(sectorForm, sector);
		
		return sector;
	}
	
	@Override
	@Transactional
	public void add(CityForm sectorForm) {
		City sector = populateCityPojo(sectorForm);
		
		getDaoFactory().getUtilityDAO().add(sector);
	}

	@Override
	@Transactional
	public void update(CityForm sectorForm) {
		City sector = populateCityPojo(sectorForm);
		
		getDaoFactory().getUtilityDAO().update(sector);
	}

	@Override
	@Transactional
	public void delete(CityForm sectorForm) {
		getDaoFactory().getUtilityDAO().delete(populateCityPojo(sectorForm));
	}

	@Override
	@Transactional
	public CityTO findById(Serializable sectorID) {
		City sector = (City) getDaoFactory().getUtilityDAO().findById(City.class,sectorID);
		
		CityTO sectorTO = new CityTO();
		BeanUtils.copyProperties(sector, sectorTO);
		
		return sectorTO; 
	}

	@Override
	@Transactional
	public List<CityTO> findByAll() {
		List<City> sectorList = getDaoFactory().getUtilityDAO().findByAll(City.class);
		
		List<CityTO> sectorFormList = new ArrayList<CityTO>();
		
		if( sectorList != null && !sectorList.isEmpty() ){
			CityTO sectorForm = null;
			for(City sector: sectorList){
				sectorForm = new CityTO();
				
				BeanUtils.copyProperties(sector, sectorForm);
				sectorFormList.add(sectorForm);
			}
		}
		
		return sectorFormList;
	}

}
